package cn.newzai.parser.proto.result.generate

import  cn.newzai.parser.proto.result._
//import scala.Enumeration.Value
import GenerateCodeType._


/**
 * Created with IntelliJ IDEA.
 * User: chenxiaohong
 * Date: 13-6-5
 * Time: 下午4:34
 * To change this template use File | Settings | File Templates.
 */
trait MessageGenerateXamlCode extends MessageGenerate{
  this : Message =>

  override def generate_code(gen_type: Value): String = {
    if( gen_type == XamlCode ){
      val enumFields = for{
        f <- fields
        if( f.data_type.isInstanceOf[EnumType] )
      } yield {
        f
      }
      val normalFields = for{
        f <- fields
        if( f.limit != Limit.repeated )
      } yield {
        f
      }
      def eachEnum( f : Field) = {
        val enumName = f.data_type match {
          case EnumType(name) => name
          case _ => ""
        }
        s"""
          | <ObjectDataProvider x:Key="${f.name}" ObjectType="{x:Type sys:Enum}" MethodName="GetValues">
          |            <ObjectDataProvider.MethodParameters>
          |                <x:Type TypeName="pbmsg:${enumName}"></x:Type>
          |            </ObjectDataProvider.MethodParameters>
          |
          |        </ObjectDataProvider>
        """.stripMargin
      }
      def eachRow( index : Int, f : Field) ={
        f.data_type match {
          case BasicType("bool") =>

            s"""
          |<TextBlock Grid.Column="0" Grid.Row="${index}">${f.toMethodName}</TextBlock>
          |<CheckBox IsChecked="{Binding Path=${f.toMethodName}}"/>
          """.stripMargin
          case BasicType(name) =>
            s"""
          |<TextBlock Grid.Column="0" Grid.Row="${index}">${f.toMethodName}</TextBlock>
          |<TextBox Grid.Column="1" Grid.Row="${index}" Text="{Binding Path=${f.toMethodName} }"></TextBox>
        """.stripMargin
          case EnumType(name)  =>
            s"""
            |<TextBlock Grid.Column="0" Grid.Row="${index}">${f.toMethodName}</TextBlock>
            |<ComboBox  Grid.Column="1" Grid.Row="${index}" ItemsSource="{Binding Source={StaticResource ${f.name} }}" SelectedItem="{Binding Path=${f.toMethodName}}"/>
            |
          """.stripMargin
          case MessageType(name) =>  ""
        }

      }

      s"""
        |<UserControl x:Class="GenCode.User${name}"
        |             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        |             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        |             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        |             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        |             xmlns:pbmsg="clr-namespace:${Message.package_name}"
        |             xmlns:local="clr-namespace:GenCode"
        |             xmlns:sys="clr-namespace:System;assembly=mscorlib"
        |             mc:Ignorable="d"
        |             d:DesignHeight="300" d:DesignWidth="300">
        |    <UserControl.Resources>
        |       ${ enumFields.map( eachEnum(_)).mkString("\n")}
        |    </UserControl.Resources>
        |    <Grid>
        |        <Grid.ColumnDefinitions>
        |            <ColumnDefinition Width="*"/>
        |            <ColumnDefinition Width="2*"/>

        |        </Grid.ColumnDefinitions>
        |        <Grid.RowDefinitions>
        |            ${normalFields.map( f => "<RowDefinition Height=\"Auto\"/>").mkString("\n")}
        |        </Grid.RowDefinitions>
        |           ${
                        val rows = for( i <- 0 until normalFields.length )yield { eachRow(i,normalFields(i))}
                        rows.mkString("\n")
                     }
        |    </Grid>
        |</UserControl>
        |
      """.stripMargin
    }else{
      super.generate_code(gen_type)
    }
  }
}



trait MessageGenerateXamlLinerCode extends MessageGenerate{
  this : Message =>

  override def generate_code(gen_type: Value): String = {
    if( gen_type == XamlCodeLiner ){

     val liners = liner_fields
     val allRowsDef = liners.map( v => """     <RowDefinition Height="Auto"/>""").mkString("\n")
     def eachRow( rowIndex : Int, path :String, field : Field) ={

       val alias = s"""<TextBlock Grid.Column="0" Grid.Row="${rowIndex}" HorizontalAlignment="Right">${field.name}:</TextBlock>"""
       val content = field.data_type match {
         case BasicType(name) => s"""<TextBox   Grid.Column="1" Grid.Row="${rowIndex}" Text="{Binding Path=${path} }"></TextBox>"""
         case EnumType(name)  =>
           val e = Enum.getEnum(name)
           e.generate_binding(path,rowIndex)
           //s"""<TextBox Grid.Column="1" Grid.Row="${rowIndex}" Text="{Binding Path=${path},Converter={StaticResource ${name.toLowerCase}2String}}"></TextBox>"""
         case _ =>""
       }
       s"""
         |${alias}
         |${content}
       """.stripMargin
       //val content =  s"""<TextBox   Grid.Column="1" Grid.Row="${rowIndex}" Text="{Binding Path=${path}"></TextBox>"""
       //val enumContent=s"""<TextBox Grid.Column="1" Grid.Row="${rowIndex}" Text="{Binding Path=${path}",Converter={StaticResource invitemode2String}}"></TextBox>"""
     }

      val allRowsDec = for( i <- 0 until liners.length ; v = liners(i)) yield { eachRow(i,v._1,v._2)}
      s"""
        |<UserControl
        |    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        |    x:Class="Focus6100Console.SilverlightControl${name}"
        |    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        |    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        |    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        |    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        |    mc:Ignorable="d"
        |    d:DesignHeight="300" d:DesignWidth="700"
        |    >
        |    <Grid>
        |       <Grid.ColumnDefinitions>
        |         <ColumnDefinition Width="0.3*" />
        |         <ColumnDefinition Width="0.7*" />
        |       </Grid.ColumnDefinitions>
        |       <Grid.RowDefinitions>
        |${allRowsDef}
        |       </Grid.RowDefinitions>
        |${allRowsDec.mkString("\n")}
        |    </Grid>
        |</UserControl>
      """.stripMargin
    }else{
      super.generate_code(gen_type)
    }
  }
}


